package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GysptKqygtj;
import com.ruoyi.gcgl.service.IGysptKqygtjService;

/**
 * 用工统计Controller
 * 
 * @author ruoyi
 * @date 2024-12-07
 */
@Controller
@RequestMapping("/gcgl/kqygtj")
public class GysptKqygtjController extends BaseController
{
    private String prefix = "gcgl/kqygtj";

    @Autowired
    private IGysptKqygtjService gysptKqygtjService;

    @RequiresPermissions("gcgl:kqygtj:view")
    @GetMapping()
    public String kqygtj(ModelMap mmap){
    	mmap.put("time", DateUtils.dateTimeNow(DateUtils.YYYY_MM));
        return prefix + "/kqygtj";
    }

    /**
     * 查询用工统计列表
     */
    @RequiresPermissions("gcgl:kqygtj:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GysptKqygtj gysptKqygtj)
    {
        startPage();
        gysptKqygtj.setDeleteFlag(1);
        gysptKqygtj.setYear(gysptKqygtj.getTimeAt().substring(0,4));
        gysptKqygtj.setMonth(Integer.parseInt(gysptKqygtj.getTimeAt().substring(5,7))+"");
        List<GysptKqygtj> list = gysptKqygtjService.selectGysptKqygtjList(gysptKqygtj);
        return getDataTable(list);
    }

    /**
     * 导出用工统计列表
     */
    @RequiresPermissions("gcgl:kqygtj:export")
    @Log(title = "用工统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GysptKqygtj gysptKqygtj)
    {
        List<GysptKqygtj> list = gysptKqygtjService.selectGysptKqygtjList(gysptKqygtj);
        ExcelUtil<GysptKqygtj> util = new ExcelUtil<GysptKqygtj>(GysptKqygtj.class);
        return util.exportExcel(list, "用工统计数据");
    }

    /**
     * 新增用工统计
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用工统计
     */
    @RequiresPermissions("gcgl:kqygtj:add")
    @Log(title = "用工统计", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GysptKqygtj gysptKqygtj)
    {
        return toAjax(gysptKqygtjService.insertGysptKqygtj(gysptKqygtj));
    }

    /**
     * 修改用工统计
     */
    @RequiresPermissions("gcgl:kqygtj:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GysptKqygtj gysptKqygtj = gysptKqygtjService.selectGysptKqygtjById(id);
        mmap.put("gysptKqygtj", gysptKqygtj);
        return prefix + "/edit";
    }

    /**
     * 修改保存用工统计
     */
    @RequiresPermissions("gcgl:kqygtj:edit")
    @Log(title = "用工统计", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GysptKqygtj gysptKqygtj)
    {
        return toAjax(gysptKqygtjService.updateGysptKqygtj(gysptKqygtj));
    }

    /**
     * 删除用工统计
     */
    @RequiresPermissions("gcgl:kqygtj:remove")
    @Log(title = "用工统计", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gysptKqygtjService.deleteGysptKqygtjByIds(ids));
    }
    
    /**
     * 考勤表项目汇总
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/ygtjPeoplePrint/{month}")
    public String ygtjPeoplePrint(@PathVariable("month") String month, ModelMap mmap){
    	//当月天数
    	int daySize = DateUtils.getDaysByDate(month);
    	List<Integer> days = new ArrayList<Integer>();
    	Double hj = 0.0;//总合计
    	for(int i =0;i<daySize;i++){
    		days.add(i+1);
    	}
    	mmap.put("daylis", days);
    	
    	//当月人员列表
    	GysptKqygtj gcYgtj = new GysptKqygtj();
    	gcYgtj.setYear(month.substring(0,4));
    	gcYgtj.setMonth(Integer.parseInt(month.substring(5,7))+"");
    	List<GysptKqygtj> peoplelis = gysptKqygtjService.selectGcYgtjList4PeoplePrint(gcYgtj);
    	
    	List<Map> replis = new ArrayList<Map>();
    	for(GysptKqygtj ygtj:peoplelis){
    		
    		GysptKqygtj l_gcYgtj = new GysptKqygtj();
    		l_gcYgtj.setDeleteFlag(1);
    		l_gcYgtj.setYear(month.substring(0,4));
    		l_gcYgtj.setMonth(Integer.parseInt(month.substring(5,7))+"");
    		l_gcYgtj.setPeopleId(ygtj.getPeopleId());
    		List<GysptKqygtj> peopleJcZc = gysptKqygtjService.selectGcYgtjList4PeopledayPrint(l_gcYgtj);
    		
    		List<String> dayJcZc = new ArrayList<String>();
    		double peopleCount = 0.0;
    		double peopleTotal = 0.0;
    		if(peopleJcZc!=null && peopleJcZc.size()>0){
    			for(int i =0;i<daySize;i++){
    				boolean flag = true;
    				Integer day = i+1;
    				for(GysptKqygtj zcObj:peopleJcZc){
    	    			if(zcObj.getDay().equals(day.toString())){
    	    				dayJcZc.add(zcObj.getMemo());
    	    				peopleCount+=1;
    	    				peopleTotal+=1;
    	    				flag = false;
    	    				break;
    	    			}
    	    		}
    				
    				if(flag){
    					dayJcZc.add("0");
    				}
    				
    			}
    		}
    		
    		//正常
    		Map rep1 = new HashMap();
    		rep1.put("peopleName", ygtj.getPeopleName());
    		rep1.put("peopleCount", peopleCount);
    		rep1.put("peopleTotal", peopleTotal);
    		rep1.put("peopleJclis", dayJcZc);
    		replis.add(rep1);
    		
    		//小计=合计
    		hj += Double.valueOf(peopleTotal);
    	}
    	
    	List<GysptKqygtj> xjlis = gysptKqygtjService.selectGcYgtjList4Print(gcYgtj);
    	List<String> dayxjs = new ArrayList<String>();
    	if(xjlis!=null && xjlis.size()>0){
	    	for(int i =0;i<daySize;i++){
				boolean flag = true;
				Integer day = i+1;
				for(GysptKqygtj zcObj:xjlis){
	    			if(zcObj.getDay().equals(day.toString())){
	    				dayxjs.add(zcObj.getMemo());
	    				flag = false;
	    				break;
	    			}
	    		}
				
				if(flag){
					dayxjs.add("0");
				}
			}
    	}
    	mmap.put("dayxjs", dayxjs);
    	
    	mmap.put("replis", replis);
    	mmap.put("years", month.substring(0,4));
    	mmap.put("month", month.substring(5,7));
    	mmap.put("hj", hj);
        return prefix + "/ygtjPeoplePrint";
    }
}